package com.swak.threads;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.codahale.metrics.MetricRegistry;
import com.swak.metrics.impl.CodahaleMetricsFactory;
import com.swak.metrics.metas.MapMeta;
import com.swak.reactivex.threads.Contexts;
import com.swak.reactivex.threads.OrderContext;
import com.swak.reactivex.threads.OrderContext.OrderAbleContext;

/**
 * 队列顺序执行
 * 
 * @author DELL
 */
public class OrderThreadPool {

	public static void main(String[] args) throws InterruptedException {
		CodahaleMetricsFactory metricsFactory = new CodahaleMetricsFactory(new MetricRegistry()).setMethodOpen(false)
				.setMethodCollectAll(false);
		Contexts.setMetricsFactory(metricsFactory);
		MetricsDumpService dump = new MetricsDumpService(metricsFactory.metricRegistry());
		OrderContext context = Contexts.createOrderContext("Test-", 10, false, 1000, TimeUnit.SECONDS);

		OrderAbleContext Run10msTask_context = context.newOrderAbleContext(5);
		OrderAbleContext Run50msTask_context = context.newOrderAbleContext(5);
		OrderAbleContext Run1sTask_context = context.newOrderAbleContext(2);
		OrderAbleContext RunBlock100msTask_context = context.newOrderAbleContext(5);
		OrderAbleContext RunBlock200msTask_context = context.newOrderAbleContext(5);
		for (int i = 0; i < 300; i++) {
			Run1sTask_context.execute(new Run1sTask());// 50 * 1000 = 50000
			RunBlock100msTask_context.execute(new RunBlock100msTask()); // 50 * 100 = 5000
			RunBlock200msTask_context.execute(new RunBlock200msTask()); // 50 * 200 = 10000
			Run10msTask_context.execute(new Run10msTask());// 50 * 10 = 500
			Run50msTask_context.execute(new Run50msTask());// 50 * 20 = 1000
		}

		// 5s 之后输出统计信息
		Thread.sleep(10000);
		List<MapMeta> metas = dump.metricsDump(null);
		for (MapMeta meta : metas) {
			System.out.println(meta);
		}
	}
}
